﻿using GrainManage.Data.Model;
using GrainManage.Data.Model.Filter;
using GrainManage.Data.Model.Table;
using GrainManage.Data.Model.View;
using GrainManage.Data.Service.Util;
using System;
using System.Collections.Generic;
using System.Data;

namespace GrainManage.Data.Service
{
    public class ContactService : BaseService, GrainManage.Data.Service.Interfaces.IContactService
    {
        public ContactService(GrainManage.Data.Access.IAccess proc) : base(proc) { }

        public bool IsContactExist(string name, string cellphone, string creator)
        {
            var result = Proc.ExecuteScalar(ProcConst.Contact_GetIDByNameCellphone, name, cellphone, creator);
            return result == null ? false : true;
        }

        public ContactTable Get(int contactID, string creator)
        {
            var ds = Proc.ExecuteQuery(ProcConst.Contact_GetByID, contactID, creator);
            return ConvertToModel<ContactTable>(GetFirstRow(ds));
        }

        public List<ContactWithUrlView> Search(bool isOr, ContactFilter filter, IPageable page, ICountable records, string creator)
        {
            DataSet contacts = null;
            if (isOr)
            {
                contacts = Proc.ExecuteQuery(ProcConst.Contact_Search_Union, filter.Name,
                                             filter.Area, filter.Address, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            }
            else
            {
                contacts = Proc.ExecuteQuery(ProcConst.Contact_Search, filter.Name,
                                        filter.Area, filter.Address, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            }
            return ConvertToModel<ContactWithUrlView>(contacts, records);
        }

        public int Insert(ContactTable contact)
        {
            Initializor.Initialize(contact, string.Empty);
            var result = Proc.ExecuteScalar(ProcConst.Contact_Insert, contact.ContactName, contact.Gender, contact.BirthDate, contact.Cellphone, contact.Telephone,
                        contact.QQ, contact.Email, contact.ImageID, contact.Area, contact.Address, contact.Remarks, contact.Creator);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        public List<int> Insert(List<ContactTable> contacts)
        {
            var contactIDs = new List<int>();
            foreach (var contact in contacts)
            {
                contactIDs.Add(Insert(contact));
            }

            return contactIDs;
        }

        public int Update(ContactTable contact)
        {
            Initializor.Initialize(contact, string.Empty);
            return Proc.Execute(ProcConst.Contact_UpdateByID, contact.ContactID, contact.ContactName, contact.Gender, contact.BirthDate, contact.Cellphone,
                contact.Telephone, contact.QQ, contact.Email, contact.ImageID, contact.Area, contact.Address, contact.Remarks, contact.Creator);
        }

        public List<int> Update(List<ContactTable> contacts)
        {
            var invalidContactIDs = new List<int>();
            foreach (var contact in contacts)
            {
                if (Update(contact) == 0)
                {
                    invalidContactIDs.Add(contact.ContactID);
                }
            }
            return invalidContactIDs;
        }

        public int Delete(int id, string creator)
        {
            int count = Proc.Execute(ProcConst.Contact_DeleteByID, id, creator);
            return count;
        }

        public List<int> Delete(List<int> idList, string creator)
        {
            var invalidList = new List<int>();
            foreach (var id in idList)
            {
                if (Delete(id, creator) == 0)
                {
                    invalidList.Add(id);
                }
            }
            return invalidList;
        }

    }
}
